﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using WindowsAPI;

namespace CSMDeamon
{
    public partial class Form1 : Form
    {

      
        public Form1()
        {
            InitIconRes();
            InitializeComponent();
        }

        List<Icon> iconList = new List<Icon>();
        int iconIndex = 0;
        int iconCount = 4;
        private void InitIconRes()
        {
            iconList.Clear();
            iconList.Add(Res.shield_r);
            iconList.Add(Res.shield_g);
            iconList.Add(Res.shield_b);
            iconList.Add(Res.shield_y);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //notifyIcon1.Icon = rotIcon ? Res.glass3d_br : Res.glass3d_rb;
            Start();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Stop();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (ForceExit == true)
            {
                return;
            }
            if (MessageBox.Show("  请确认是否退出进程守护程序？   ", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                e.Cancel = true;
            }
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        #region Monitor Process
       string processName = ConfigurationManager.AppSettings["ProcessName"];
       string processPath = ConfigurationManager.AppSettings["ProcessPath"];

       int detectInterval = int.Parse(ConfigurationManager.AppSettings["DetectInterval"]) * 1000;
       int watchDogInterval = int.Parse(ConfigurationManager.AppSettings["WatchDogInterval"]) * 1000;
       int maxTimeOut = int.Parse(ConfigurationManager.AppSettings["MaxTimeOut"]);


        AppMonitor appMonitor = null;
        private void Start()
        {
            appMonitor = new AppMonitor();
            appMonitor.ProcessName = this.processName;
            appMonitor.ProcessPath = this.processPath;
            appMonitor.DetectInterval = this.detectInterval;
            appMonitor.WatchDogInterval = this.watchDogInterval;
            appMonitor.MaxTimeOut = this.maxTimeOut;

            appMonitor.Start();

        }

        private void Stop()
        {
            appMonitor.Stop();
        }

        #endregion


        int dwData;
        byte[] lpData;
        protected override void WndProc(ref Message m)
        {
            //接收目标进程的看门狗消息
            if (m.Msg == WinAPI.WM_COPYDATA)
            {
                WinAPI.ReceivCopyData(ref m, out dwData, out lpData);

                if ((dwData == WinAPI.WM_WATCHDOG) && (appMonitor != null))
                {
                    appMonitor.ResetWatch();  
                    iconIndex++;
                    iconIndex = iconIndex % iconCount;
                  
            
                }
                else
                {
                    ProcessCopyData(dwData,lpData);
                }

            }
            else
            {
                base.WndProc(ref m);
            }

        }
        bool ForceExit = false;
        private void ProcessCopyData(int dwData, byte[] lpData)
        {
            switch (dwData)
            {
                case (int)CmdType.SetDelayTime:
                    if (lpData.Length > 2)
                    {
                        appMonitor.MaxTimeOut = lpData[0] + (lpData[1] << 8);
                    }
                    break;
                case (int)CmdType.Exit:
                    ForceExit = true;
                    this.Close();
                    break;
            }
        }

        bool rotIcon = false;
        private void timer1_Tick(object sender, EventArgs e)
        {
            rotIcon = !rotIcon;

            notifyIcon1.Icon = iconList[iconIndex];

            label1.ForeColor = rotIcon ? Color.LightGray : Color.White;
            label1.Text = appMonitor.State; 

        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void mnuShow_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;

            WinAPI.SetForegroundWindow(this.Handle);
            WinAPI.OpenIcon(this.Handle);  
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            mnuShow.PerformClick();
        }








    }
}
